package com.pig4cloud.pig.ai.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pig4cloud.pig.ai.api.entity.RelationEntity;
import com.pig4cloud.pig.ai.service.AtlasService;
import com.pig4cloud.pig.ai.service.RelationService;
import com.pig4cloud.pig.common.core.util.R;
import com.pig4cloud.pig.common.log.annotation.SysLog;
import com.pig4cloud.pig.common.security.util.SecurityUtils;
import com.pig4cloud.plugin.excel.annotation.ResponseExcel;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.http.HttpHeaders;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 图谱表管理
 *
 * @author xuzhuang
 * @date 2024-06-13 13:52:04
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/relation" )
@Tag(description = "relation" , name = "图谱表管理" )
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class RelationController {

    private final RelationService relationService;

	/**
	 * 返回树形菜单集合
	 * @param datasourceName 数据源名称
	 * @return 树形菜单
	 */
	@GetMapping(value = "/tree")
	public R getTree(long topoId,String datasourceName) {
		return R.ok(relationService.selectTree(topoId,datasourceName));
	}

	@GetMapping(value = "/graph")
	public R graph(long topoId) {
		return R.ok(relationService.selectTreeTopoId(topoId));
	}


	/**
	 * 通过ID查询
	 * @param id ID
	 * @return RelationEntity
	 */
	@GetMapping("/{id}")
	public R getById(@PathVariable Long id) {
		return R.ok(relationService.getById(id));
	}

	/**
	 * 查询全部图谱
	 */
	@GetMapping("/list")
	public R list() {
		return R.ok(relationService.list());
	}

	/**
	 * 添加
	 * @param relationEntity 实体
	 * @return success/false
	 */
	@SysLog("添加图谱")
	@PostMapping
	@PreAuthorize("@pms.hasPermission('relation_add')")
	public R save(@Valid @RequestBody RelationEntity relationEntity) {
		relationEntity.setCreateTime(LocalDateTime.now());
		relationEntity.setCreateBy(SecurityUtils.getUser().getName());
		return R.ok(relationService.save(relationEntity));
	}

	/**
	 * 删除
	 * @param id ID
	 * @return success/false
	 */
	@SysLog("删除图谱")
	@DeleteMapping("/{id}")
	@PreAuthorize("@pms.hasPermission('relation_del')")
	public R removeById(@PathVariable Long id) {
		return R.ok(relationService.removeRelationEntityById(id));
	}

	/**
	 * 编辑
	 * @param relationEntity 实体
	 * @return success/false
	 */
	@SysLog("编辑图谱")
	@PutMapping
	@PreAuthorize("@pms.hasPermission('relation_edit')")
	public R update(@Valid @RequestBody RelationEntity relationEntity) {
		relationEntity.setUpdateTime(LocalDateTime.now());
		relationEntity.setUpdateBy(SecurityUtils.getUser().getName());
		return R.ok(relationService.updateById(relationEntity));
	}

	/**
	 * 查收子级列表
	 * @return 返回子级
	 */
	@GetMapping(value = "/getDescendantList/{id}")
	public R getDescendantList(@PathVariable Long id) {
		return R.ok(relationService.listDescendant(id));
	}

}